package com.artron.mediaartron.ui.fragment.made;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.artron.baselib.base.BaseStaticFragment;
import com.artron.baselib.utils.UIUtils;
import com.artron.mediaartron.R;
import com.artron.mediaartron.data.entity.FrameData;
import com.artron.mediaartron.data.entity.SameSizeFrame;
import com.artron.mediaartron.ui.adapter.SelectionHorizontalPagerAdapter;
import com.artron.mediaartron.ui.fragment.factory.CustomSelectionVerticalFragmentFactory;

import java.util.List;

import butterknife.BindView;
import me.kaelaela.verticalviewpager.VerticalViewPager;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-09-21.
 */

public class CustomSelectionVerticalFragment extends BaseStaticFragment {

    private static final String TAG = "CustomSelectionVertical";

    @BindView(R.id.CustomSelectionHorizontalFragment_pager)
    protected VerticalViewPager mVerticalViewPager;
    private List<FrameData> mData;
    private int mCurrentPosition;
    private SelectionHorizontalPagerAdapter mAdapter;

    public static CustomSelectionVerticalFragment newInstance(SameSizeFrame line) {
        CustomSelectionVerticalFragment fragment = new CustomSelectionVerticalFragment();
        fragment.init(line);
        return fragment;
    }

    public static CustomSelectionVerticalFragment newInstance() {
        return new CustomSelectionVerticalFragment();
    }

    public void init(SameSizeFrame line) {
        this.mData = line.getData();
        for (FrameData frameData : mData) {
            frameData.setPagerHeight(line.getHeight());
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_custom_selection_vertical;
    }

    @Override
    protected void initView() {
        if (isSecondLoad) {
            return;
        }

        mAdapter = new SelectionHorizontalPagerAdapter(getChildFragmentManager(), mData);
        mVerticalViewPager.setAdapter(mAdapter);

        mVerticalViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                if (position == mCurrentPosition) {
                    return;
                }

                CustomSelectionVerticalFragmentFactory factory =
                        CustomSelectionVerticalFragmentFactory.getInstance();
                if (factory != null) {
                    for (CustomSelectionVerticalFragment vFragment : factory.getFragments()) {
                        vFragment.setCurrentPosition(position);
                    }
                }

                FrameData frameData = mData.get(position);
                Fragment parentFragment = getParentFragment();
                if (parentFragment instanceof CustomSelectionFragment) {
                    ((CustomSelectionFragment) parentFragment).setText(frameData.getPrice(),
                            frameData.getShapeType(), frameData.getShowSize());
                }

                SelectionHorizontalFragment child = (SelectionHorizontalFragment)
                        mAdapter.getFragment(position);
                if (child != null) {
                    child.updateDrawable();
                }
            }
        });
        mVerticalViewPager.setCurrentItem(mCurrentPosition);

        mVerticalViewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    UIUtils.hiddenKeyboard(getView());
                }
                return false;
            }
        });
    }

    public void setCurrentPosition(int position) {
        if (mVerticalViewPager != null) {
            mVerticalViewPager.setCurrentItem(position);
        }
        this.mCurrentPosition = position;
    }

    public void updateChildImageViewContent(String path) {
        try {
            FragmentManager childFragmentManager = getChildFragmentManager();
            List<Fragment> fragments = childFragmentManager.getFragments();
            for (Fragment fragment : fragments) {
                if (fragment instanceof SelectionHorizontalFragment) {
                    ((SelectionHorizontalFragment) fragment)
                            .updateImageViewContent(path);
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "updateChildImageViewContent: ", e);
        }
    }

    public VerticalViewPager getVerticalViewPager() {
        return mVerticalViewPager;
    }

    public void updateTips(FrameData data, boolean isUpdateTips) {
        for (int i = 0; i < mData.size(); i++) {
            if (mVerticalViewPager.getCurrentItem() == i && mData.get(i).equals(data)) {
                ((CustomSelectionFragment) getParentFragment()).updateTips(isUpdateTips);
            }
        }
    }

}
